package com.zhuj.register;

import com.zhuj.core.Global;
import com.zhuj.core.util.JedisUtil;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2018/1/11.
 */
public class Register {

    public static void registerService(Set<String> services,int port) throws UnknownHostException {
        JedisUtil jedisUtil = JedisUtil.getInstance();
        Map<String,String> map = new HashMap<String, String>();
        String service_url = InetAddress.getLocalHost().getHostAddress()+":"+port;
        for (String key:services){
            map.put(key,service_url);
        }
        jedisUtil.HASH.hmset(Global.NRPC_REG,map);
    }

    public static Map.Entry<String,Integer> getServiceUrl(String service){
        JedisUtil jedisUtil = JedisUtil.getInstance();
        String regUrl = jedisUtil.HASH.hmget(Global.NRPC_REG,service).get(0);
        Map<String,Integer> result = new HashMap<String, Integer>();
        String[] regUrls = regUrl.split(":");
        result.put(regUrls[0],Integer.parseInt(regUrls[1]));
        return result.entrySet().iterator().next();
    }

}
